package cs4280.ass2.admin.action;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang.time.DateUtils;

import cs4280.ass2.entity.Movie;
import cs4280.ass2.exception.ValidateException;
import cs4280.ass2.persistence.MovieDao;
import cs4280.ass2.util.MovieUtils;

public class MovieNewAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String START_DATE_FORMAT = "dd/MM/yyyy";

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.sendRedirect(req.getContextPath()+ "/admin/movie/new.jsp");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String name = StringUtils.trim(req.getParameter("name"));
		String description = StringUtils.trim(req.getParameter("description"));
		String director = StringUtils.trim(req.getParameter("director"));
		String cast = StringUtils.trim(req.getParameter("cast"));
		String duration = StringUtils.trim(req.getParameter("duration"));
		String category = StringUtils.trim(req.getParameter("category"));
		String language = StringUtils.trim(req.getParameter("language"));
		String trailerUrl = StringUtils.trim(req.getParameter("trailerUrl"));
		String imageUrl = StringUtils.trim(req.getParameter("imageUrl"));
		String startDateStr = StringUtils.trim(req.getParameter("startDate"));

		Date startDate = null;
		try {
			startDate = DateUtils.parseDate(startDateStr, new String[] { START_DATE_FORMAT });
		} catch (Exception e) {
			req.setAttribute("message.error", "message.error.invalid.dateformat");
			getServletContext().getRequestDispatcher("/admin/movie/new.jsp").forward(req, resp);
			return;
		}

		Movie movie = new Movie();
		movie.setImageUrl(imageUrl);
		movie.setName(name);
		movie.setDescription(description);
		movie.setDirector(director);
		movie.setCast(cast);
		movie.setDuration(NumberUtils.toInt(duration, 0));
		movie.setCategory(category);
		movie.setLanguage(language);
		movie.setStartDate(startDate);
		movie.setTrailerUrl(trailerUrl);
		movie.setCreateDate(new Date());
		movie.setCreateUser(0);
		movie.setModifyDate(new Date());
		movie.setModifyUser(0);

		try {
			MovieUtils.validate(movie);
			MovieDao.getInstance().addMovie(movie);
			req.setAttribute("message.info", "message.info.movie.addsuccess");
			getServletContext().getRequestDispatcher("/admin/movie/list.do").forward(req, resp);
		} catch (ValidateException e) {
			req.setAttribute("message.error", e.getMessage());
			getServletContext().getRequestDispatcher("/admin/movie/new.jsp").forward(req, resp);
		}

	}
}
